<?php

namespace Qwadmin\Model;
use Think\Model;

class MemberModel extends Model{
    /**
     *  添加用户
     *  @param array $$arr 用户数组
     *  @return int 如果用户添加成功返回用户新的ID号，否则返回0，如果用户已经存在，则返回-1
     */
    public function addUser( $arr){
        if(!$this->exist($arr['user'])){   
            return M("member")->add($arr);
        }
        return -1;
    }
    
    /**
     *  用户名是否存在
     */
    public function exist($username){
        $userInfo = M("member")->where(['user' => $username])->find();      
        if(empty($userInfo)){
            return false;
        }
        return true;
    }
    
   /**
    *  根据用户ID查找
    *  @param array|string $where 数组或条件表达式
    */
    public function findUser($where){
        return M("member")->where($where)->find();
    }
    
    /**
     *  显示用户列表
     *  @param array $where 分页条件
     *  @param int $pagesize 每页显示记录数
     */
    public function lists($where , $pagesize = 20 ){
        import('ORG.Util.Page');// 导入分页类
        $userModel = M("member");
        $count = $userModel->where($where)->count("uid");
        $pagebar = new \Think\Page($count, $pagesize);
        $show = $pagebar->show();
        
        $list = $userModel->where($where)->order("is_enable desc , uid desc")->limit($pagebar->firstRow ." ," . $pagebar->listRows)->select();
        return [
            'list' => $list,
            'page'  => $show
        ];
    }
    
    /**
     *  编辑用户资料
     *  @param array $member 用户信息
     *  @param int $uid 用户ID
     */
    public function editUser($member , $uid){
        return M("member")->where(['uid' => $uid])->save($member);
    }
    
    /**
     *  删除用户，并把权限也去掉
     */
    public function delUser($uid){
        $mRow = M("member")->where(['uid' => $uid])->delete();
        $aRow = M("auth_group_access")->where(['uid' => $uid])->delete();
        return $mRow + $aRow ;
    }
    
    /**
     *  用户余额增加
     *  @param int $uid 用户ID
     *  @param float $amount 增加金额
     */
    public function setIncAmount($uid , $amount){
       return  M("member")->where(['uid' => $uid])->setInc('money', $amount);
    }
    
    /**
     *  用户余额减少
     *  @param int $uid 用户ID
     *  @param float $amount 减少金额
     */
    public function setDecAmount($uid , $amount){
      return   M("member")->where(['uid' => $uid])->setDec('money', $amount);
    }
    
    /**
     *  设置或取消锁定
     *  @param int $uid 用户ID
     *  @param int $value 结果
     */
    public function setLock($uid , $value){
        return $this->editUser(['is_lock' => $value], $uid);
    }
    
	/**
	*  获取用户的权限
	*  @param int $uid 用户ID
	*  @return int 用户的权限
	**/
	public function getUserGid($uid){
		return M('auth_group_access')->where(['uid'=>$uid])->getField('group_id');//获取登陆账户的权限
	}
	
	/**
	*  获取用户的锁定、提现权限
	*  @param int $uid 用户ID
	*  @return int 用户的权限
	**/
	public function getUserJurisdiction($uid,$ret = []){
		if(count($ret)==0){
			$ret = ['if_user_lock' => 1 , 'if_user_cash' => 1];
		}
		$user = $this->findUser(['uid'=>$uid]);
		if($user['if_user_lock'] !=1){
			$ret['if_user_lock'] = 2;
		}
		if($user['if_user_cash'] !=1){
			$ret['if_user_cash'] = 2;
		}
		if($user['level'] > 2 && ($ret['if_user_lock'] == 1 || $ret['if_user_cash'] == 1)){
			$p_user = $this->getUserJurisdiction($user['pid'],$ret);//查询其上级的权限
			if($p_user['if_user_lock'] !=1){
				$ret['if_user_lock'] = 2;
			}
			if($p_user['if_user_cash'] !=1){
				$ret['if_user_cash'] = 2;
			}
		}
		return $ret;
	}
	
	
	/**
	* 新增用户统计
	**/
	public function User_count($uid,$level,$start,$end,$ret = []){
		if(count($ret)==0){
			$ret = [
					'man'		=> 0,//男性
					'woman'		=> 0,//女性
					'Unknown'	=> 0,//未知
					'subscribe'	=> 0,//关注
					'pay_num'	=> 0,//支付数
			];
		}
		$cache_User_count = S('User_count_'.$uid.'_'.$level.'_'.$start.'_'.$end);
		//$cache_User_count=false;
		if(empty($cache_User_count)){
			$ret = $this->User_sel($uid,$start,$end,$ret);
			if($level < 4){//当其级别大于二级推广商时
				$member_list = M('member')->field("uid,level")
					->where('pid = '.$uid)
					->order('uid desc')
					->select();
				foreach($member_list as $value){
					$ret = $this->User_count($value['uid'],$value['level'],$start,$end,$ret);
				}
			}
			$time = time();
			$now_day=strtotime(date('Y-m-d'));
			$day_time = 3600*24*30-($time-$now_day)+60;//获取缓存时间 缓存到第二日凌晨1分
			if(!empty($start) && $start==$now_day){
				$day_time = 1;
			}
			S('User_count_'.$uid.'_'.$level.'_'.$start.'_'.$end,$ret,$day_time);
		}else{
			$ret = $cache_User_count;
		}
		return $ret;
	}
	function User_sel($uid,$start,$end,$ret){
		$where = [
					'refid'			=> $uid,
					'sex'			=> 1
				];
		if(!empty($start)){
			$where['time'][] = [
				'EGT',
				$start
			];
		}
		if(!empty($end)){
			$where['time'][] = [
				'LT',
				$end
			];
		}
		$ret['man'] += M('user')->where($where)->count('uid');
		$where['sex'] = 2;
		$ret['woman'] += M('user')->where($where)->count('uid');
		$where['sex'] = ['not in',[1,2]];
		$ret['Unknown'] += M('user')->where($where)->count('uid');
		
		unset($where['sex']);
		$where['subscribe'] = 1;
		$ret['subscribe'] += M('user')->where($where)->count('uid');
		
		unset($where['subscribe']);
		$where['integral'] = ['gt',0];
		$ret['pay_num'] += M('user')->where($where)->count('uid');
		
		return $ret;
	}
  
  	/**
     * 获取下级
     */
    public function Next_member($uid){
        return M("member")->field("uid")->where(['pid' => $uid])->select();
        
    }
}
